﻿#include "u_menubar.h"

#include "Window/Common/volumebutton.h"

#include "Window/System/u_syslog_page.h"
#include "Window/System/u_user_page.h"
#include "Window/System/u_userpwd_page.h"
#include "Window/System/u_userlogin_page.h"
#include "Window/System/u_equipment_page.h"
#include "Window/System/u_config_system.h"
#include "Window/Data/u_quality_page.h"
#include "Window/Data/u_quality_alarm_page.h"
#include "Window/Data/u_quailty_sim.h"
#include "Window/System/ippage.h"

#pragma execution_character_set("utf-8")
enum
{
    ID_BUT_HOME=10000,
    ID_BUT_RECEIVER,

    ID_ACT_USER,
    ID_ACT_SYSLOG,
    ID_ACT_EQUIPMENT,
    ID_ACT_IP,
    ID_ACT_CONFIG,
    ID_ACT_QUALITY,
    ID_ACT_QUALITYALARM,
    ID_ACT_SIMHISTORY,

    ID_ACT_PWD,
    ID_ACT_LOGIN,
    ID_ACT_LOGOUT,
    ID_ACT_ABOUT,
    ID_ACT_THEME_BLUE,
    ID_ACT_THEME_BLACK,

};


MenuBar::MenuBar(QWidget *parent) : MyWidget(parent)
{
    this->parent = parent;

    this->InitWidget();//初始化
    this->SetupConnections();//connect
    this->Initialize();
    this->UserChange();//隐藏
    widget=(QWidget*)(this->parent->parent());
    //this->OnBtnMenuMaxClicked();
    //this->setCursor(QCursor(Qt::PointingHandCursor));
}

void MenuBar::InitWidget(){
    int height = 60;

    QHBoxLayout* menuLayot = this->InitMenu();

    labLogo = new QLabel();
    labLogo->setMinimumSize(100,height);
    labLogo->setMaximumSize(100,height);
    labLogo->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//labei对齐方式
    labLogo->setPixmap(QPixmap::fromImage(QImage(":/images/System/logo.png").scaled(96, 36,Qt::KeepAspectRatio)));//放入图片  按96:36填充Label
    labLogo->setStyleSheet("background: rgba(0,0,0,0.06)");//以文本形式设置背景

    //---------------------------------------------------

    butHome = new QPushButton(QChar(0xe62b) + QString(" 主页"));
    butHome->setObjectName(QString::number(ID_BUT_HOME));
    butHome->setCheckable(true);//可以点击
    butHome->setChecked(true);//保持点击状态

    butReceiver = new QPushButton(QChar(0xe7ba) + QString(" 接收机"));
    butReceiver->setObjectName(QString::number(ID_BUT_RECEIVER));
    butReceiver->setCheckable(true);

    butData = new QPushButton(QString("数据查询 ") + QChar(0xe824));
    butData->setCursor(QCursor(Qt::PointingHandCursor));//光标形式
    butData->setStyleSheet(QString("padding: 5px -3px 5px 10px;"));

    butSystem = new QPushButton(QString("系统设置 ") + QChar(0xe824));
    butSystem->setCursor(QCursor(Qt::PointingHandCursor));
    butSystem->setStyleSheet(QString("padding: 5px -3px 5px 10px;"));

    butCos = new QPushButton(QString("系数设置  "));
    butCos->setCursor(QCursor(Qt::PointingHandCursor));
    butCos->setStyleSheet(QString("padding: 5px -3px 5px 10px;"));


    butList.append(butHome);
    butList.append(butReceiver);

    //-------------------------------------------------------------

    actQuality = new QAction("历史功率");  actQuality->setObjectName(QString::number(ID_ACT_QUALITY));
    actQualityAlarm = new QAction("状态历史");     actQualityAlarm->setObjectName(QString::number(ID_ACT_QUALITYALARM));
    actSimHis=new QAction("模拟历史");     actSimHis->setObjectName(QString::number(ID_ACT_SIMHISTORY));

    actUser = new QAction("用户管理");  actUser->setObjectName(QString::number(ID_ACT_USER));
    actSysLog = new QAction("日志记录");   actSysLog->setObjectName(QString::number(ID_ACT_SYSLOG));
    actEquipment = new QAction("发射机管理");   actEquipment->setObjectName(QString::number(ID_ACT_EQUIPMENT));
    actIp=new QAction("网口管理");     actIp->setObjectName(QString::number(ID_ACT_IP));
    actConfig = new QAction("系统设置");  actConfig->setObjectName(QString::number(ID_ACT_CONFIG));

    QMenu *menuData = new QMenu();
    menuData->setObjectName("bigMenu");
    menuData->setWindowFlags(menuData->windowFlags() | Qt::FramelessWindowHint);//设置窗口默认Qt属性且产生一个无窗口边框的窗口，此时用户无法移动该窗口和改变它的大小

    menuData->addAction(actQuality);
    menuData->addSeparator();
    menuData->addAction(actSimHis);
    menuData->addAction(actQualityAlarm);

    QMenu *menuSystem = new QMenu();
    menuSystem->setObjectName("bigMenu");
    menuSystem->setWindowFlags(menuSystem->windowFlags() | Qt::FramelessWindowHint);
    menuSystem->addAction(actEquipment);
    menuSystem->addAction(actIp);
    menuSystem->addAction(actUser);
    menuSystem->addSeparator();
    menuSystem->addAction(actSysLog);

    butData->setMenu(menuData);
    butSystem->setMenu(menuSystem);


    //---------------------------------------------------

    QHBoxLayout *butLayout = new QHBoxLayout();
    butLayout->setMargin(0);
    butLayout->setSpacing(10);
    butLayout->addWidget(butHome);
    //butLayout->addWidget(butReceiver);
    butLayout->addWidget(butData);
    butLayout->addWidget(butSystem);
    QLabel *label=new QLabel();
    label->setStyleSheet("color:rgb(255,255,255);background-color:rgb(26,115,232);font: 75 20px '黑体';");
    label->setText("50/10Kw发射机控制系统");
    butLayout->addStretch(50);
    //butLayout->addWidget(label);
    //butLayout->addWidget(butCos);


    //---------------------------------------------------

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->setContentsMargins(0,0,10,0);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(labLogo);
    mainLayout->addLayout(butLayout);
    mainLayout->addStretch();//水平弹簧
    mainLayout->addLayout(menuLayot);

    this->setMinimumHeight(height);
    this->setMaximumHeight(height);
    this->setLayout(mainLayout);
}

void MenuBar::SetupConnections(){
    connect(butHome, SIGNAL(clicked()), this, SLOT(OnBtnClicked()));//设置按钮选中

    connect(actQuality, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actSimHis, SIGNAL(triggered()),this, SLOT(OnActionClicked()));
    connect(actQualityAlarm, SIGNAL(triggered()), this, SLOT(OnActionClicked()));

    connect(actUser, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actSysLog, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actEquipment, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actIp,SIGNAL(triggered()),this,SLOT(OnActionClicked()));
    connect(actConfig, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
}

void MenuBar::Initialize(){

}



void MenuBar::OnBtnClicked(){

    QPushButton *m_B = dynamic_cast<QPushButton*>(sender());
    int id = m_B->objectName().toInt();

    switch(id)
    {
    case ID_BUT_HOME:
        this->OnShowPage(0);
        break;
    case ID_BUT_RECEIVER:
        this->OnShowPage(1);
        break;
    }

}

void MenuBar::OnActionClicked(){
    QAction *m_A = dynamic_cast<QAction*>(sender());
    int id = m_A->objectName().toInt();

    switch(id)
    {
    case ID_ACT_SIMHISTORY://模拟历史
        (new SimHistory(this->parent))->show();
        break;
    case ID_ACT_USER:
        (new UserPage(this->parent))->exec();
        break;
    case ID_ACT_SYSLOG:
        (new SysLogPage(this->parent))->exec();
        break;
    case ID_ACT_EQUIPMENT:
        (new EquipmentPage(this->parent))->exec();
        break;
    case ID_ACT_IP:
        (new IpPage(this->parent))->exec();
        break;
    case ID_ACT_CONFIG:
        (new ConfigSystem(this->parent))->exec();
        break;
    case ID_ACT_QUALITY://历史功率
        (new QualityPage(this->parent))->show();
        break;
    case ID_ACT_QUALITYALARM://状态历史
        (new QualityAlarmPage(this->parent))->show();
        break;

    case ID_ACT_PWD:
        (new UserPwdPage(this->parent))->exec();
        break;
    case ID_ACT_LOGIN:
        (new UserLoginPage(this->parent))->exec();
        break;
    case ID_ACT_LOGOUT:
        if(MyGUI::ShowMessageBoxQuesion("确定退出吗", this->parent)){
            globalData->SaveSysLog("退出系统", 1);

            User user;
            globalData->user = user;
            emit globalData->UserChange();
        }
        break;
    case ID_ACT_ABOUT:
    {
        TMessageBox *msg = new TMessageBox(this->parent);
        msg->SetMessage("系统名称：50/10Kw发射机控制系统\n"
                        "系统版本：V 1.0.0.0\n"
                        "开发厂商：石家庄开发区泰顺电子通讯有限公司", 0);
        msg->SetIcon(QChar(0xe76f));
        msg->setTitle("关于系统");
        msg->resize(350, 150);
        msg->exec();
    }
        break;
    case ID_ACT_THEME_BLUE:
        MyGUI::SetStyle(":/qss/blue.qss");
        break;
    case ID_ACT_THEME_BLACK:
        MyGUI::SetStyle(":/qss/black.qss");
        break;
    }
}

void MenuBar::OnShowPage(int index){
    for(QPushButton *but : butList){
        but->setChecked(false);
    }
    butList.at(index)->setChecked(true);
    emit ShowPage(index);
}

void MenuBar::UserChange(){

    bool b = globalData->user.login;
    actPwd->setVisible(b);
    actLogin->setVisible(!b);
    actLogout->setVisible(b);
}


QFrame* MenuBar::CreateLine(){
    QFrame *line = new QFrame();//qframe设置边框
    line->setFrameShape(QFrame::VLine);//定义阴影
    line->setFrameShadow(QFrame::Sunken);//定义边框形状
    return line;
}

QHBoxLayout* MenuBar::InitMenu(){
    this->max = false;
    this->mousePressed = true;
    this->installEventFilter(this);

    butMenuMin = new QToolButton();
    butMenuMin->setObjectName("btnMenu_Min");
    butMenuMin->setToolTip("最小化");
    butMenuMin->setText(QChar(0xe731));

    butMenuMax = new QToolButton();
    butMenuMax->setObjectName("btnMenu_Max");
    butMenuMax->setToolTip("最大化");
    butMenuMax->setText(QChar(0xe7ea)); //0xe7ea

    butMenuClose = new QToolButton();
    butMenuClose->setObjectName("btnMenu_Close");
    butMenuClose->setToolTip("关闭");
    butMenuClose->setText(QChar(0xe666));

    butUser = new QToolButton();
    butUser->setObjectName("btnMenu");
    butUser->setToolTip("用户");
    butUser->setText(QChar(0xe82f));
    butUser->setCursor(QCursor(Qt::PointingHandCursor));//光标凡在这个小部件时的形状
    butUser->setPopupMode(QToolButton::InstantPopup);

    //------------------------------------------------

    actPwd = new QAction("修改密码");       actPwd->setObjectName(QString::number(ID_ACT_PWD));
    actLogin = new QAction("用户登录");     actLogin->setObjectName(QString::number(ID_ACT_LOGIN));
    actLogout = new QAction("退出系统");    actLogout->setObjectName(QString::number(ID_ACT_LOGOUT));
    actAbout = new QAction("关于系统");     actAbout->setObjectName(QString::number(ID_ACT_ABOUT));
    actThemeBlue = new QAction("蓝色样式");     actThemeBlue->setObjectName(QString::number(ID_ACT_THEME_BLUE));
    actThemeBlack = new QAction("黑色样式");     actThemeBlack->setObjectName(QString::number(ID_ACT_THEME_BLACK));

    bool b = globalData->user.login;
    actPwd->setVisible(b);//是否可见
    actLogin->setVisible(!b);
    actLogout->setVisible(b);

    QMenu *menuUser = new QMenu();
    menuUser->setWindowFlags(menuUser->windowFlags() | Qt::FramelessWindowHint);
    //menu->setAttribute(Qt::WA_TranslucentBackground);
    menuUser->addAction(actPwd);
    menuUser->addAction(actLogin);
    menuUser->addAction(actLogout);
    menuUser->addSeparator();//添加分隔符
    //menuUser->addAction(actThemeBlue);
    //menuUser->addAction(actThemeBlack);
    menuUser->addSeparator();
    menuUser->addAction(actAbout);
    butUser->setMenu(menuUser);

    //------------------------------------------------

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->setMargin(0);
    btnLayout->setSpacing(5);
    btnLayout->addWidget(butUser);
    btnLayout->addWidget(butMenuMin);
    btnLayout->addWidget(butMenuMax);
    btnLayout->addWidget(butMenuClose);

    connect(butMenuMin, SIGNAL(clicked()), this, SLOT(OnBtnMenuMinClicked()));
    connect(butMenuMax, SIGNAL(clicked()), this, SLOT(OnBtnMenuMaxClicked()));
    connect(butMenuClose, SIGNAL(clicked()), this, SLOT(OnBtnMenuCloseClicked()));

    connect(actPwd, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actLogin, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actLogout, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actAbout, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actThemeBlue, SIGNAL(triggered()), this, SLOT(OnActionClicked()));
    connect(actThemeBlack, SIGNAL(triggered()), this, SLOT(OnActionClicked()));

    return btnLayout;
}

bool MenuBar::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonDblClick) {
        this->OnBtnMenuMaxClicked();
        return true;
    }
    return QObject::eventFilter(obj, event);
}

void MenuBar::mouseMoveEvent(QMouseEvent *e)
{
    if (mousePressed && (e->buttons() && Qt::LeftButton)) {
        if(max){
            return;
        }

        widget->move(e->globalPos() - mousePoint);//globalPos()返回事件发生时光标的全局位置
        e->accept();//接受事件
    }
}

void MenuBar::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        mousePressed = true;
        mousePoint = e->globalPos() - widget->pos();//
        e->accept();
    }
}

void MenuBar::mouseReleaseEvent(QMouseEvent *)
{
    mousePressed = false;
}

void MenuBar::OnBtnMenuCloseClicked()
{
    if(MyGUI::ShowMessageBoxQuesion("确定关闭吗")){
        globalData->SaveSysLog("关闭系统", 1);
        this->parent->close();
        qApp->exit();
    }
}

void MenuBar::OnBtnMenuMaxClicked()
{
    if (max) {//如果是最大还原到正常值并设置居中显示
        //this->parent->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
        widget->showNormal();
        this->butMenuMax->setText(QChar(0xe7ea));
        this->butMenuMax->setToolTip("最大化");
        MyGUI::FormInCenter(this->parent);
    } else {//如归不是最大 设置到最大值
        //this->parent->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
        widget->showFullScreen();
        this->butMenuMax->setText(QChar(0xe68d));
        this->butMenuMax->setToolTip("还原");
    }
    max = !max;
}

void MenuBar::OnBtnMenuMinClicked()
{
    qDebug()<<"menubar"<<this->parent;
    //QWidget* widget= (QWidget*)(this->parent->parent());
    widget->showMinimized();
}

void MenuBar::paintEvent(QPaintEvent *event){
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QWidget::paintEvent(event);
}

